package com.whfc.ms.api.md.controller;

import com.whfc.base.dto.AppWeatherDTO;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.env.dto.EnvDataLogAnalysisDTO;
import com.whfc.env.dto.EnvDustDTO;
import com.whfc.env.dto.EnvDustDataDTO;
import com.whfc.env.dto.EnvWarnRecordDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.md.service.MdEnvBoardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 数据大屏 -环境
 * @author qzexing
 * @version 1.0
 * @date 2021/11/22 9:35
 */
@RestController
@RequestMapping("/ms/api/md/board/env")
public class MdEnvBoardController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MdEnvBoardService mdEnvBoardService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId) {
        logger.info("大屏-环境-设备列表, loginer:{}, deptId:{}", user.getUsername(), deptId);
        List<EnvDustDTO> list = mdEnvBoardService.deviceList(deptId);
        return ResultUtil.success(new ListData<>(list));
    }

    @GetMapping("latest")
    public Result getLatestEnvData(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                   @RequestParam("dustId") Integer dustId) {
        logger.info("大屏-环境-获取环境最新数据,loginUser:{}, dustId:{}", loginUser.getUsername(), dustId);
        EnvDustDataDTO data = mdEnvBoardService.getLatestData(dustId);
        return ResultUtil.success(data);
    }

    @GetMapping("log")
    public Result getEnvDataLog(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                @RequestParam("dustId") Integer dustId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {
        logger.info("board环境-获取环境历史数据,loginUser:{}, dustId:{}, startTime:{}, endTime:{}",
                loginUser.getUsername(), dustId, startTime, endTime);
        List<EnvDustDataDTO> data = mdEnvBoardService.getDustDataLog(dustId, startTime, endTime);
        return ResultUtil.success(new ListData<>(data));
    }

    @GetMapping(value = "log/analysis")
    public Result envDataLogAnalysis(@SessionAttribute(SessionAttr.MS_USER) SysUser loginUser,
                                     @RequestParam("dustId") Integer dustId,
                                     @RequestParam("type") String type,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("startTime") Date startTime,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam("endTime") Date endTime) {
        logger.info("board环境-环境历史数据分析,loginUser:{},dustId:{}, envType:{}, startTime:{},endTime:{}",
                loginUser.getUsername(), dustId, type, startTime, endTime);
        EnvDataLogAnalysisDTO envDataLogAnalysisDTO = mdEnvBoardService.airDataLogAnalysis(dustId, type, startTime, endTime);
        return ResultUtil.success(envDataLogAnalysisDTO);
    }


    @GetMapping("/warn/record/list")
    public Result recordList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("dustId") Integer dustId,
                             @RequestParam(value = "ruleType") Integer ruleType,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime") Date startTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime") Date endTime) {
        logger.info("board环境-环境报警记录，loginer:{},dustId:{},ruleType:{},pageNum:{},pageSize:{},startTime:{},endTime:{}",
                user.getUsername(), dustId, ruleType, pageNum, pageSize, startTime, endTime);
        PageData<EnvWarnRecordDTO> pageData = mdEnvBoardService.getEnvDeviceWarnRecordList(dustId, ruleType, pageNum,
                pageSize, startTime, endTime);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/weather")
    public Result getWeather(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId) {
        logger.info("board环境-晴雨表，loginer:{}, deptId:{}", user.getUsername(), deptId);
        List<AppWeatherDTO> list = mdEnvBoardService.getWeatherList(deptId);
        return ResultUtil.success(new ListData<>(list));
    }

}
